<?php
/**
 *  NevuxBB - Free Forum Software
 *  Copyright (C) 2008, 2010 NevuxBB (No one specific author)*  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * NevuxBB Cache Engine
 * File: cache_class.php
 * Todo: add more caching methods
 *
 */

class CacheEngine {
	
	/**
	* Determines if the cache is on or not
	*
	* @return boolean
	* @access public
	*/	
	public function isCacheOn() {
		if(GlobalSettings::getSetting('cache_enabled') == 1) {
			// is enabled
			return true;
		} else {
			// doesn't exist or isn't
			return false;
		}
	}
	
	/**
	* Returns the cache method set by an admin
	*
	* @return numeric
	* @access private
	*/	
	private function cacheMethod() {
		// return cache method value
		return GlobalSettings::getSetting('cache_method');
	}
	
	/**
	* Gets cache directory
	*
	* @return string
	* @access private
	*/	
	private function getCacheFileLocation() {
		// return the default caching of files location
		return BASE_PATH . '/cache/';
	}
	
	/**
	* Determines if a cache item exists and hasn't expired
	*
	* @param string $item
	* @param string $expiry
	* @return boolean
	* @access public
	*/	
	public function CacheExist($item, $expiry = NULL) {
		switch(self::CacheMethod()) {
			case 'file':
				// need to load this from the file
				$file_loc = self::getCacheFileLocation() . 'cha_' . md5($item);
				
				// does it exist?
				if(file_exists($file_loc)) {
					// has it expired?
					if(!empty($expiry) && is_numeric($expiry)) {
						if((time()) > (filemtime($file_loc)+$expiry)) {
							// remove it, useless
							KernelBaseSystem::removeFile($file_loc);
							
							// return false;
							return false;
						} else {
							// return true
							return true;
						}
					} else {
						// does exist and doesn't expire
						return true;
					}
				} else {
					// doesn't exist
					return false;
				}

				// break
				break;
			default:
				return false;
				break;
		}
	}
	
	/**
	* Fetches a cache item
	*
	* @param string $item
	* @return string or boolean
	* @access public
	*/	
	public function fetchCache($item) {
		switch(self::CacheMethod()) {
			case 'file':
				// need to load this from the file
				$file_loc = self::getCacheFileLocation() . 'cha_' . md5($item);
				
				// does it exist?
				if(file_exists($file_loc)) {
					// does exist
					if(KernelBaseSystem::fetchFile($file_loc) === false) {
						return false;
					} else {
						$rawd = unserialize(KernelBaseSystem::fetchFile($file_loc));
						
						if($rawd === false) {
							// failed to unserialize, trigger error
							trigger_error('<strong>Cache Failure:</strong> Unable to unserialize this item ' . $file_loc, E_USER_WARNING);
					
							// return false to previous area
							return false;
						} else {
							// successful, return raw data
							return $rawd;
						}	
					}
				} else {
					// doesn't exist
					return false;
				}
				
				// break
				break;
			default:
				// unknown method
				return false;
				
				// break
				break;
		}
	}
	
	/**
	* Removes a cache item
	*
	* @param string $item
	* @return boolean
	* @access public
	*/	
	public function removeCache($item) {
		// generate item location
		$file_loc = self::getCacheFileLocation() . 'cha_' . md5($item);
			
		// remove it by request
		KernelBaseSystem::removeFile($file_loc);
		
		// completed task
		return true;
	}
	
	/**
	* Adds a new item into the cache
	*
	* @param string $item
	* @param string $data
	* @return boolean
	* @access public
	*/	
	public function addCache($item, $data) {
		switch(self::CacheMethod()) {
			case 'file':
				// need to write to this file
				$file_loc = self::getCacheFileLocation() . 'cha_' . md5($item);
				
				// does it exist?
				if(file_exists($file_loc)) {
					// does exist, need to remove it
					if(KernelBaseSystem::removeFile($file_loc)) {
						// returned true, is removed
					} else {
						// can't be removed
						$cccd = false;
					}
				}
				
				// can we do this
				if($cccd === false) {
					// can't do it
					trigger_error('<strong>Cache Failure:</strong> Failed to write new cache file for ' . $item, E_USER_WARNING);
					
					// return false to previous area
					return false;
				} else {
					// write it
					// it's safe to proceed
					if(KernelBaseSystem::writeFile($file_loc, serialize($data))) {
						// has been written successfully
						return true;
					} else {
						// failed, trigger error
						trigger_error('<strong>Cache Failure:</strong> Failed to write new cache file for ' . $item, E_USER_WARNING);
						
						// return false
						return false;
					}
				}
				
				// break
				break;
			default:
				// unknown cache method
				return false;
				
				// break
				break;
		}
	}
	
	/**
	* Caches an image file
	*
	* @param string $location
	* @param string $to_location
	* @param string $new_name
	* @param boolean $is_from_web
	* @return boolean
	* @access public
	*/	
	public function CacheImageFile($location, $to_location, $new_name, $is_from_web = NULL) {
		// are we caching a web file?
		if($is_from_web) {
			// we are going to try and fetch this file
			$f_conts = KernelBaseSystem::fetchWebFile($location);
			
			// did it work?
			if($f_conts === (false || NULL)) {
				// didn't work, return false
				return false;
			} else {
				// make file location
				$file_loc = BASE_PATH . '/' . $to_location . '/' . $new_name;
				
				// did work, write this new data to a specific location
				if(KernelBaseSystem::writeFile($file_loc, $f_conts)) {
					// worked, we wrote the file successfully
					return true;
				} else {
					// failed, trigger error
					trigger_error('<strong>Cache Failure:</strong> Failed to write file from URL to ' . $item, E_USER_WARNING);
					
					// didn't work, unable to write file
					return false;
				}
			}
		} else {
			// currently, this function only supports the writing
			// of files from a URL
			return false;
		}
	}
}

?>